var authentication
var globalCommentPageNum = window.sessionStorage.getItem("globalCommentPageNum")
if (globalCommentPageNum == null) {
    globalCommentPageNum = 1
}
function login() {
    var uname = document.getElementById("adminname").value
    if (uname != "admin") {
        alert("仅限管理员登陆！！！")
        return
    }
    var pwd = document.getElementById("adminpwd").value
    console.log("aaaa")
    $.ajax({
        url: "/login/toLogin",
        method: "POST",
        data: {
            uname: uname,
            upwd: pwd
        },
        success: function (response) {
            // 处理后端响应数据
            console.log("aaaa", response)
            if (response.code === 200) {
                window.sessionStorage.setItem("username", response.user.username)
                window.sessionStorage.setItem("userid", response.user.user.uid)
                document.getElementById("beforelogin").style.display = "none";
                document.getElementById("loginok").style.display = "block";
                document.getElementById("username").value = response.user.username
                window.sessionStorage.setItem("authentication", response.authentication)
                authentication = window.sessionStorage.getItem("authentication")
            }

        },
        error: function (response) {
            console.log(response)
        }
    })
}

function selectPage(pageNum) {
    console.log("按钮被点击了！" + pageNum)
    globalPageNum = pageNum;
    window.sessionStorage.setItem("globalPageNum", pageNum)
    $.ajax({
        url: "news/getAdminAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
}

function getDetailNews(nid) {
    $.ajax({
        url: "news/getDetailNews",
        method: "GET",
        data: {
            nid: nid
        },
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
            getComment(nid)
            // document.getElementById("cauthorNoLogin")
        }
    })
}

function queryPageByTopic(tid) {
    console.log("按钮被点击了！" + tid);
    globalTid = tid
    window.sessionStorage.setItem("globalTid", tid)
    globalPageNum = 1
    $.ajax({
        url: "news/getAdminAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
}

function search() {
    globalKey = document.getElementById("mysearch").value
    console.log("按钮被点击了！" + globalKey);
    globalPageNum = 1
    window.sessionStorage.setItem("globalKey", globalKey)
    $.ajax({
        url: "news/getAdminAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
}

function toEdit(nid) {
    if (window.sessionStorage.getItem("username") === 'admin') {
        var authentication = window.sessionStorage.getItem("authentication")
        window.sessionStorage.setItem("nid", nid)
        $.ajax({
            url: "news/toEdit",
            method: "GET",
            data: {
                nid: nid,
            },
            headers: {
                authentication: authentication
            },
            success: function (response) {
                console.log(response.code)
                if (response.code === '101') {
                    alert(response.msg)
                } else {
                    document.getElementById("adminNews").innerHTML = response
                    getComment(nid)

                }
            }
        })
    } else {
        layer.msg("认证超市或未登录，请登录！！！")
    }

}

function getComment(nid) {
    $.ajax({
        url: "comment/getAdminDetailComment",
        method: "GET",
        data: {
            nid: nid,
            pageNum:globalCommentPageNum
        },
        success: function (response) {
            document.getElementById("admincomment").innerHTML = response
        }
    })
}
function selectCommentPage(pageNum) {
    console.log("按钮被点击了！" + pageNum)
    globalCommentPageNum = pageNum;
    window.sessionStorage.setItem("globalCommentPageNum", pageNum)
    var nid = window.sessionStorage.getItem("nid")
    $.ajax({
        url: "comment/getAdminDetailComment",
        method: "GET",
        data: {
            "pageNum": globalCommentPageNum,
            "nid":nid
        },
        success: function (response) {
            document.getElementById("admincomment").innerHTML = response
        }
    })
}
function updateNews() {
    if (window.sessionStorage.getItem("username") === 'admin') {
        var authentication = window.sessionStorage.getItem("authentication")
        var formElement = document.getElementById('myform');
        var formData = new FormData(formElement);
        formData.append("authentication", authentication)
        // 发送 AJAX 请求，将图片数据上传到服务器
        $.ajax({
            url: "/news/updateNews",
            type: "POST",
            contentType: false,
            processData: false,
            data: formData,
            headers: {
                authentication: authentication
            },
            success: function (response) {
                // 上传成功
                console.log(response)
                if (response.code === 200) {
                    layer.msg("操作成功！！！")
                    selectPage(globalPageNum)
                } else layer.msg(response.msg)
            }
        });
    } else {
        layer.msg("认证超市或未登录，请登录！！！")
    }


}

function saveNews() {
    if (window.sessionStorage.getItem("username") === 'admin') {
        var authentication = window.sessionStorage.getItem("authentication")
        var formElement = document.getElementById('myform');
        var formData = new FormData(formElement);
        if (formData.get("ntitle") != '' && formData.get("ncontent") != '') {
            // 发送 AJAX 请求，将图片数据上传到服务器
            $.ajax({
                url: "/news/saveNews",
                type: "POST",
                contentType: false,
                processData: false,
                data: formData,
                headers: {
                    authentication: authentication
                },
                success: function (response) {
                    // 上传成功
                    console.log(response)
                    if (response.code === 200) {
                        layer.msg("操作成功！！！")
                        selectPage(1)
                    } else alert(response.msg)
                }
            });
        } else {
            layer.msg("请至少输入标题和内容!!!")
        }
    } else {
        layer.msg("认证超市或未登录，请登录！！！")
    }

}

function addTopic() {
    if (window.sessionStorage.getItem("username") == 'admin') {
        var authentication = window.sessionStorage.getItem("authentication")
        var formElement = document.getElementById('myform');
        var formData = new FormData(formElement);
        if (formData.get("tname") != null && formData.get("tname") != '') {
            var tid = formData.get("tid")
            if (tid != null && tid != '') {
                // 发送 AJAX 请求，将图片数据上传到服务器
                $.ajax({
                    url: "/topic/editTopic",
                    type: "POST",
                    data: formData,
                    contentType:false,
                    processData: false,
                    headers: {
                        authentication: authentication
                    },
                    success: function (response) {
                        // 上传成功
                        console.log(response)
                        if (response.code === 200) {
                            layer.msg("操作成功！！！")

                        } else alert(response.msg)
                    }
                });
            } else {
                $.ajax({
                    url: "/topic/addTopic",
                    type: "POST",
                    data: formData,
                    contentType:false,
                    processData: false,
                    headers: {
                        authentication: authentication
                    },
                    success: function (response) {
                        // 上传成功
                        console.log(response)
                        if (response.code === 200) {
                            layer.msg("操作成功！！！")
                            toaddTopic()
                        } else alert(response.msg)
                    }
                });
            }
        } else {
            layer.msg("请输入主题！！！")

        }
    } else {
        layer.msg("认证超市或未登录，请登录！！！")
    }
}

function delComment(cid) {
    if (window.sessionStorage.getItem("username") == 'admin') {
        var authentication = window.sessionStorage.getItem("authentication")
        if (sessionStorage.getItem("username") != null) {
            var nid = window.sessionStorage.getItem("nid")
            $.ajax({
                url: "/comment/delComments",
                type: "GET",
                data: {
                    cid: cid,
                    nid: nid
                },
                headers: {
                    authentication: authentication
                },
                success: function (response) {
                    // 上传成功
                    console.log(response)
                    if (response.code === 200) {
                        layer.msg("删除成功！！！")
                        getComment(nid)
                    }

                }
            })
        }
    } else {
        alert("请先登录！！！")
    }
}

function toIndex() {
    $.ajax({
        url: "news/getAdminAllNews",
        method: "GET",
        data: {
            "pageNum": globalPageNum,
            "tid": globalTid,
            "keywords": globalKey
        },
        success: function (response) {

            document.getElementById("adminNews").innerHTML = response
        }
    })
    if (sessionStorage.getItem("usrename") == null) {
        document.getElementById("beforelogin").style.display = "block";
        document.getElementById("loginok").style.display = "none";
        // document.getElementById("username").value = response.user.username
    }
}

function toaddNew() {
    $.ajax({
        url: "news/toAddNews",
        method: "GET",
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
}

function toaddTopic() {
    $.ajax({
        url: "topic/toTopicAdd",
        method: "GET",
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
}

function toeditTopic() {
    $.ajax({
        url: "topic/toTopicEdit",
        method: "GET",
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
}

var globalTopic

function toTopicEditDetail(tid) {
    $.ajax({
        url: "topic/toTopicEditDetail",
        method: "GET",
        async: false,
        data: {
            tid: tid
        },
        success: function (response) {
            document.getElementById("adminNews").innerHTML = response
        }
    })
    globalTopic = document.getElementById("mytopic").value
}

function resetTopic() {
    document.getElementById("mytopic").value = globalTopic
}

function delTopic(tid) {
    if (sessionStorage.getItem("username") === 'admin') {
        var authentication = window.sessionStorage.getItem("authentication")
        $.ajax({
            url: "topic/delTopic",
            method: "GET",
            data: {
                tid: tid
            },
            headers: {
                authentication: authentication
            },
            success: function (response) {
                if (response.code == 200) {
                    layer.msg("删除成功！！！")
                    toeditTopic()
                } else alert(response.msg)
            }
        })
    } else {
        layer.msg("认证超市或未登录，请登录！！！")
    }
}

function delNews(nid) {
    var authentication = window.sessionStorage.getItem("authentication")
    if (window.sessionStorage.getItem("username") === 'admin') {
        $.ajax({
            url: "news/delNews",
            method: "GET",
            data: {
                nid: nid
            },
            headers: {
                authentication: authentication
            },
            success: function (response) {
                if (response.code == 200) {
                    layer.msg("删除成功！！！")
                    toIndex()
                } else alert(response.msg)
            }
        })
    } else {
        layer.msg("认证超市或未登录，请登录！！！")
    }
}

function loginOut() {
    window.sessionStorage.clear()
    toIndex()
}
function resetForm() {
    document.getElementById("myform").reset();
}
